define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'data/storehouse/index' + location.search,
                    add_url: 'data/storehouse/add',
                    edit_url: 'data/storehouse/edit',
                    del_url: 'data/storehouse/del',
                    multi_url: 'data/storehouse/multi',
                    table: 'storehouse',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'hid'},
                        // {field: 'number', title: __('Number')},
                        {field: 'name', title: __('Name')},
                        {field: 'code', title: __('唯一标识')},
                        {field: 'remark', title: __('Remark')},
                        // {field: 'shop_supplier_name', title: __('商城账号')},
                        // {field: 'shop_supplier_pwd', title: __('商城密码')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_default', title: __('默认仓库'),formatter:function(value,row){
                                if(value == 1){
                                    return '<i class="fa fa-vine" aria-hidden="true"></i>';
                                }
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'setdefault',
                                    title: __('设为默认'),
                                    classname: 'btn btn-xs btn-black btn-click editstatus',
                                    icon: 'fa fa-vine',
                                    hidden:function(row){
                                        if(row.is_default == 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var ids =$(this).parents('tr').find('.hid').html()
                                        var url = Config.this_url+'/data/storehouse/setdefault?ids='+ids
                                        $.post(url,{},function (res) {
                                            layer.msg(res.msg)
                                            if(res.code == 1){
                                                $('.btn-refresh').click()
                                            }
                                        })
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                // console.log(value)
                                // console.log(row)
                                var that = $.extend({}, this);
                                // if(row.ystatus !=1){
                                //     $(table).data("operate-edit", null);
                                // }
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});